package com.dts.evaluate.utils;

import android.content.Context;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

/**
 * Created by user on 2016/6/29.
 */
public class IdUtil {

    /**
     * 获取唯一标识  ，这里用的mac地址
     *
     * @param context
     * @return
     */
    public static String getDeviceId(Context context) {
       return getAndroidId(context);
    }

    /**
     * 获取设备mac地址  需要打开wifi
     * @return
     */
    private static String getMac() {
        String macSerial = null;
        String str = "";

        try {
            Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);

            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();// 去空格
                    break;
                }
            }
        } catch (IOException ex) {
            // 赋予默认值
            ex.printStackTrace();
        }
        if(StringUtils.isEmpty(macSerial)){
            return "";
        }
        return macSerial.replace(":","").toUpperCase();
    }

    /**
     * 获取androidid
     *
     * @param context
     * @return
     */
    private static String getAndroidId(Context context) {

        String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        if(StringUtils.isEmpty(androidId)){
            return "";
        }
        return androidId.toUpperCase();
    }

    /**
     * 获取imei
     * @param context
     * @return
     */
    private static String getIMEI(Context context){
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String phoneDeviceId = tm.getDeviceId();
        if(StringUtils.isEmpty(phoneDeviceId)){
            return "";
        }
        return phoneDeviceId.toUpperCase();
    }


    /**
     * 获取android.os.Build.SERIAL
     * @param context
     * @return
     */
    private static String getBuildSerial(Context context){
        return android.os.Build.SERIAL;
    }

}
